<template>
  <a-tree-select
    style="width: 100%"
    :dropdown-style="{ maxHeight: '400px', width: '300px', overflow: 'auto' }"
    placeholder="请选择"
    allow-clear
    :value="[nbjgh||'']"
    tree-default-expand-all
    :treeData="nbjghLists"
    :getPopupContainer="
      (triggerNode) => {
        return triggerNode.parentNode || document.body
      }
    "
    @select="select"
  >
  </a-tree-select>
</template>

<script>
import { queryNbjghList } from '@/api/api'
export default {
  name: 'Mechanism',
  model: {
    prop: 'nbjgh',
    event: 'respond',
  },
  props: {
    nbjgh: {
      type: String,
    },
    type: String,
  },
  data() {
    return {
      nbjghList: [],
      nbjghLists: [],
    }
  },
  created() {
    this.selectUser()
  },
  methods: {
    select(value) {
      this.$emit('respond', value)
    },
    // 查询机构
    selectUser(value) {
      queryNbjghList({ nbjgh: 'nbjgh', bankNoCd : value }).then((res) => {
        if (res.code === 200) {
          let list = res.result || []
          let treeList =
            list.filter((item) => item.PARENTID === '' || item.PARENTID === null || item.PARENTID === undefined) || []
          if (list.length > 0) {
            this.loopTree(treeList, list)
          }
          this.nbjghLists = this.loop(treeList)
          this.nbjghList = this.loop(list)
        } else {
          this.$message.warning('服务器出错')
        }
      })
    },
    loopTree(treeList, list) {
      treeList.map((item) => {
        item.children = list.filter((el) => el.PARENTID === item.ID) || []
        if (item.children.length > 0) {
          this.loopTree(item.children, list)
        }
      })
    },
    loop(list) {
      return list.map((item, index, arr) => {
        let c = { label: item.NAME || item.DEPARTNAME, value: item.CODE || item.ORGCODE, id: item.CODE || item.ORGCODE }
        if (item.children) {
          c.children = this.loop(item.children)
        }
        return c
      })
    },
  },
}
</script>

<style>
</style>